package com.xy.servicemanage.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Map;

public class SerializeUtil {

	/**
	 * 序列化对象
	 * 
	 * @throws IOException
	 */

	public static byte[] serializeObject(Object object) {

		try {
			ByteArrayOutputStream saos = new ByteArrayOutputStream();

			ObjectOutputStream oos = new ObjectOutputStream(saos);

			oos.writeObject(object);

			oos.flush();

			return saos.toByteArray();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	/**
	 * 反序列化对象
	 * 
	 * @throws IOException
	 * 
	 * @throws ClassNotFoundException
	 */

	public static Object deserializeObject(byte[] buf) {

		try {
			Object object = null;

			ByteArrayInputStream sais = new ByteArrayInputStream(buf);

			ObjectInputStream ois = new ObjectInputStream(sais);

			object =   ois.readObject();

			return object;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
